package com.aaa.service.sds.impl;

import com.aaa.entity.PageSortEntity;
import com.aaa.entity.sds.TestResultSds;
import com.aaa.entity.sds.ViewTestResultSds;
import com.aaa.dao.sds.ViewTestResultSdsDao;
import com.aaa.service.sds.ViewTestResultSdsService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sun.org.apache.bcel.internal.generic.DDIV;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (ViewTestResultSds)表服务实现类
 *
 * @author makejava
 * @since 2022-09-06 15:05:20
 */
@Service("viewTestResultSdsService")
public class ViewTestResultSdsServiceImpl implements ViewTestResultSdsService {
    @Resource
    private ViewTestResultSdsDao viewTestResultSdsDao;


    /**
     * @param pageSortEntity
     * @param viewTestResultSds
     * @return
     * @create by: 张恒
     * @description: 查询
     * @create time: 2022/9/6 16:54
     */
    @Override
    public Page queryViewTestResultSds(PageSortEntity pageSortEntity, ViewTestResultSds viewTestResultSds) {
        //分页加排序
        PageHelper.startPage(pageSortEntity.getPage(), pageSortEntity.getLimit(), pageSortEntity.getOrderString());
        List<Map<String, Object>> mapList = viewTestResultSdsDao.queryViewTestResultSds(viewTestResultSds);
        Page page = (Page) mapList;
        return page;
    }


    /**
     * @param id
     * @return
     * @create by: 张恒
     * @description: 删除
     * @create time: 2022/9/6 16:54
     */
    @Override
    public boolean deleteViewTestResultSds(Integer[] id) {
        return viewTestResultSdsDao.deleteViewTestResultSds(id);
    }


    /**
     * @param viewTestResultSds
     * @return
     * @create by: 张恒
     * @description: 根据id查询
     * @create time: 2022/9/6 19:15
     */
    @Override
    public ViewTestResultSds queryViewTestResultSdsById(ViewTestResultSds viewTestResultSds) {
        return viewTestResultSdsDao.queryViewTestResultSdsById(viewTestResultSds);
    }

    /**
     * @param
     * @return
     * @create by: 张恒
     * @description: 查询人数
     * @create time: 2022/9/6 20:36
     */
    @Override
    public Map<String, Integer> queryViewTestResultSdsCount() {
        //无抑郁
        int normal = 0;
        //轻微
        int mild = 0;
        //中度
        int moderate = 0;
        //重度
        int severe = 0;
        List<ViewTestResultSds> viewTestResultSds = viewTestResultSdsDao.queryViewTestResultSdsCount();
        for (ViewTestResultSds viewTestResultSd : viewTestResultSds) {
            Double front = viewTestResultSd.getFront();
            Double against = viewTestResultSd.getAgainst();
            Double sum = front + against;
            if (sum < 50) {
                normal++;
            }
            if (sum < 60) {
                mild++;
            }
            if (sum < 70) {
                moderate++;
            } else {
                severe++;
            }
        }
        Map<String, Integer> map = new HashMap<>();
        map.put("normal", normal);
        map.put("mild", mild);
        map.put("moderate", moderate);
        map.put("severe", severe);
        return map;
    }

    @Override
    public String querySdsById(Integer id) {
        ViewTestResultSds viewTestResultSds = viewTestResultSdsDao.querySdsById(id);
        Double sum = viewTestResultSds.getSum();
        if (sum <= 50) {
            return "views/sds/SdsNormal.jsp";
        } else if (sum < 63) {
            return "views/sds/SdsMild.jsp";
        } else if (sum < 70) {
            return "views/sds/SdsModerate.jsp";
        } else {
            return "views/sds/SdsSevere.jsp";
        }
    }

    @Override
    public List<ViewTestResultSds> queryViewTestResultSdsBySum(Integer sum) {
        return viewTestResultSdsDao.queryViewTestResultSdsBySum(sum);
    }


    /**
     * @create by: 张恒
     * @description: 查看测试详情
     * @create time: 2022/9/13 9:01
     * @param pageSortEntity
     * @param id
     * @return
     */
    @Override
    public Page queryResultByQuestion(PageSortEntity pageSortEntity, TestResultSds id) {
        PageHelper.startPage(pageSortEntity.getPage(), pageSortEntity.getLimit(),pageSortEntity.getOrderString());
        List<TestResultSds> viewPersonResultSds = viewTestResultSdsDao.queryResultByQuestion(id);
        Page page = (Page) viewPersonResultSds;
        return page;
    }
}
